<?php

/**
 * 普通网页程序基类
 */

class App_Page extends App_Web
{
    protected $headTmpl = 'head/head_page.html';
    protected $tailTmpl = 'tail/tail_page.html';
    protected $title = "";

    protected $csslist = array();
    protected $headjslist = array();
    protected $footjslist = array();
    protected $asyncjslist = array();

    function __construct($lgmode)
    {
        parent::__construct($lgmode);
        $this->setCssJs();
    }

    protected function checkAuth()
    {
        parent::checkAuth();

        if (empty($this->_uid) && 'pri' == $this->lgmode)
        {
            // 需要登陆的页面
            $this->delegateTo("login" . DS . "login.php");
            exit;
        }
    }

    protected function setTitle($title)
    {
        $this->title = $title;
    }

    protected function setCssJs()
    {
        $this->csslist = array(
            'css/base.css',
            'css/panel.css',
        );
        $this->headjslist = array(
            'js/base.js',
            'js/env.js',
            'js/core/jQuery.js',
        );
        $this->asyncjslist = array(	// data-ajax需要
            "js/core/ajax/Dialog.js",
            "js/core/ajax/Request.js"
        );
    }

    protected function addCss($cssList)
    {
        $this->csslist = array_merge($this->csslist , $cssList);
    }

    protected function addHeadJs($jsList)
    {
    	if (is_string($jsList))
    	{
    		$jsList = array($jsList);
    	}
        $this->headjslist = array_merge($this->headjslist , $jsList);
    }

    protected function addFootJs($jsList)
    {
    	if (is_string($jsList))
    	{
    		$jsList = array($jsList);
    	}
        $this->footjslist = array_merge($this->footjslist , $jsList);
    }

    protected function setHeadTmpl($tmpl)
    {
        $this->headTmpl = $tmpl;
    }

    protected function setTailTmpl($tmpl)
    {
        $this->tailTmpl = $tmpl;
    }

    protected function outputHttp()
    {
        if (!headers_sent())
        {
            header("Content-Type: text/html; charset=".SYS_CHARSET);
        }
    }

    protected function outputHead()
    {
        if ($this->_user)
        {
            $unreadMsgNum = $this->message_alert->getUnreadNum($this->_uid);
            $this->smarty->assign('unreadMsgNum', $unreadMsgNum);
            $this->smarty->assign('_user', $this->_user);
        }
        $this->title = empty($this->title) ? 'chinamall' : ($this->title . ' - chinamall');
        $this->smarty->assign("title", $this->title);
        $this->smarty->assign('cssHtml', Tool_CssJs::getCssHtml($this->csslist));
        $this->smarty->assign('jsHtml', Tool_CssJs::getJsHtml($this->headjslist));
        $this->smarty->display($this->headTmpl);
    }

    protected function outputTail()
    {
        $jsHtml = Tool_CssJs::getJsHtml($this->footjslist, true);
		$asyncJsHtml = Tool_CssJs::getAsyncJsHtml($this->asyncjslist);
        $this->smarty->assign('jsHtml', $asyncJsHtml."\n".$jsHtml);

        $jsEnv = array("wwwHost" => WWW_HOST);
        $this->smarty->assign("jsEnv", $jsEnv);
		$this->smarty->assign("inDev", defined('IN_DEV') && IN_DEV ? 1:0);
        $this->smarty->display($this->tailTmpl);
    }

    protected function showError($ex)
    {
        echo "<!-- \n";
        var_export($ex);
        echo "-->\n";
        $GLOBALS['t_exception'] = $ex;
        $this->delegateTo("common" . DS .  "500.php");
        Tool_Log::debug('@app_page', "code:".$ex->getCode()."\nerror:" . $ex->getMessage() . "\n" . var_export($ex->getTrace(),true));
        exit;
    }
}

?>